﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Chat
{
    public static class ChatCommand
    {
        private static readonly String[] Commands = 
        {
            "",
            "/join ",
            "/leave ",
            "/me ",
            "/room "
        };

        private static readonly bool[] CommandArgumentRequirement = 
        {
            false,
            true,
            true,
            true,
            true
        };

        public enum Command
        {
            Empty = 0, // when there is no command in message
            JoinRoom, // /join <room>
            LeaveRoom, // /leave <room>
            Me, // /me <action>
            MessageForRoom, // /room <room> <message> - message for room
            CommandsCount
        }

        private static bool IsValidCommand(Command command)
        {
            return (command >= 0 && command < Command.CommandsCount);
        }

        public static bool CommandRequiresArgument(Command command)
        {
            if (!IsValidCommand(command))
            {
                return false;
            }

            return CommandArgumentRequirement[(int) command];
        }

        public static String PrefixForCommand(Command command)
        {
            if (!IsValidCommand(command))
            {
                return String.Empty;
            }

            return Commands[(int) command];
        }

        public static Command GetCommand(String message)
        {
            for (Command command = Command.Empty + 1; command < Command.CommandsCount; command++)
            {
                String prefix = PrefixForCommand(command);

                if (prefix.Length < message.Length &&
                    message.Substring(0, prefix.Length).Equals(prefix))
                {
                    return command;
                }
            }

            return Command.Empty;
        }

        public static String GetCommandArgument(String message)
        {
            Command command = GetCommand(message);

            if (!CommandRequiresArgument(command))
            {
                return String.Empty;
            }

            String prefix = PrefixForCommand(command);
            int argumentEndingPosition = message.Substring(prefix.Length).IndexOf(' ');

            if (argumentEndingPosition == -1)
            {
                return message.Substring(prefix.Length);
            }

            return message.Substring(prefix.Length, argumentEndingPosition);
        }

        public static String GetMessage(String message)
        {
            switch (GetCommand(message))
            {
                case Command.Empty:
                    return message;

                case Command.JoinRoom:
                    return String.Empty;

                case Command.LeaveRoom:
                    return String.Empty;

                case Command.Me:
                    return String.Empty;

                case Command.MessageForRoom:
                    String result = String.Empty;
                    String prefix = PrefixForCommand(GetCommand(message));
                    int argumentEndingPosition = message.Substring(prefix.Length).IndexOf(' ');
                    return message.Substring(prefix.Length + argumentEndingPosition + 1);

                default:
                    return String.Empty;
            }
        }
    }
}
